const App = {
    data() {
        return {
            login: {
                telephone: '',
                password: ''
            },
            register: {
                telephone: '',
                password: '',
                c_password: '',
                nickname: '',
                email: '',
                username: ''
            }
        };
    },
    methods: {
        clickLogin: function () {
            const login = this.login;
            if (isEmpty(login.telephone, login.password)) {
                this.tipsMessage(1, "禁止留空");
                return
            }
            axios.post("/api/User/isNoLogin/LoginServlet", {
                tel: login.telephone,
                password: login.password
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return;
                }
                this.tipsMessage(data.code, data.message);
                setTimeout(() => {
                    location.assign('center');
                }, 500)
            });
        },
        clickRegister: function () {
            const res = this.register;
            const tel = res.telephone;
            const password = res.password;
            const cpassword = res.c_password;
            const nickname = res.nickname;
            const email = res.email;
            const username = res.username;
            if (isEmpty(tel, password, cpassword, nickname, email, username)) {
                this.tipsMessage(1, "禁止留空");
                return;
            }
            if (!isTelephone(tel)) {
                this.tipsMessage(1, "手机格式错误");
                return;
            }
            if (isEmail(email)) {
                this.tipsMessage(1, "邮箱格式错误");
                return;
            }
            if (password !== cpassword) {
                this.tipsMessage(1, "请确认密码是否输入正确");
                return;
            }
            axios.post("/api/User/isNoLogin/RegisterServlet", {
                tel: tel,
                password: password,
                cpassword: cpassword,
                nickname: nickname,
                email: email,
                username: username
            }).then((req) => {
                const data = req.data;
                if (data.code > 0) {
                    this.tipsMessage(data.code, data.error);
                    return
                }
                this.tipsMessage(data.code, data.message);
                setTimeout(() => {
                    location.reload()
                }, 500);
            });

        },
        tipsMessage: function (code, message) {
            let type = '';
            if (code === 0) {
                type = 'success'
            } else if (code === 1) {
                type = 'info'
            } else if (code === 2) {
                type = 'warning'
            } else if (code === 3) {
                type = 'error'
            }
            this.$message({
                type: type,
                message: message
            });
        },
    }

};
const app = Vue.createApp(App);
app.use(ElementPlus);
app.mount("#app");